package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.dispatch.client.NewOrderFeignClient;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.DriverService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.entity.driver.DriverInfo;
import com.atguigu.daijia.model.form.driver.DriverFaceModelForm;
import com.atguigu.daijia.model.form.driver.UpdateDriverAuthInfoForm;
import com.atguigu.daijia.model.vo.driver.DriverAuthInfoVo;
import com.atguigu.daijia.model.vo.driver.DriverLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {

    @Autowired
    private DriverInfoFeignClient driverInfoFeignClient;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String login(String code) {
        Result<Long> result = driverInfoFeignClient.login(code);
        if(result.getCode() != 200){
            throw new GuiguException(result.getCode(), result.getMessage());
        }
        Long driverId = result.getData();
        if(driverId==null){
            throw new GuiguException(ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }
        String token = UUID.randomUUID().toString().replace("-", "");
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token, driverId.toString(),RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
        return token;
    }

    @Override
    public DriverLoginVo getDriverLoginInfo(Long driverId) {

        Result<DriverLoginVo> result = driverInfoFeignClient.getDriverLoginInfo(driverId);

        if(result.getCode() != 200){
            throw new GuiguException(result.getCode(), result.getMessage());
        }

        DriverLoginVo driverLoginVo = result.getData();

        if(null == driverLoginVo) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        return driverLoginVo;
    }

    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        return driverInfoFeignClient.getDriverAuthInfo(driverId).getData();
    }

    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        return driverInfoFeignClient.updateDriverAuthInfo(updateDriverAuthInfoForm).getData();
    }

    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm).getData();
    }

    @Override
    public Boolean isFaceRecognition(Long diverId) {
        return driverInfoFeignClient.isFaceRecognition(diverId).getData();
    }

    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm diverFaceModelForm) {
        return driverInfoFeignClient.verifyDriverFace(diverFaceModelForm).getData();
    }

    @Autowired
    private LocationFeignClient locationFeignClient;

    @Autowired
    private NewOrderFeignClient newOrderFeignClient;

    @Override
    public Boolean startService(Long diverId) {
        //判断司机认证状态
        DriverLoginVo driverLoginVo = driverInfoFeignClient.getDriverLoginInfo(diverId).getData();
        if(driverLoginVo.getAuthStatus() !=2){
            throw new GuiguException(ResultCodeEnum.AUTH_ERROR);
        }
        //判断当日是否人脸识别
        Boolean isFaceRecognition = driverInfoFeignClient.isFaceRecognition(diverId).getData();
        if(!isFaceRecognition){
            throw new GuiguException(ResultCodeEnum.FACE_ERROR);
        }
        //修改司机接单状态
        driverInfoFeignClient.updateServiceStatus(diverId,1);
        //删除司机geo数据
        locationFeignClient.removeDriverLocation(diverId);
        //清空队列
        newOrderFeignClient.clearNewOrderQueueData(diverId);
        return true;
    }

    @Override
    public Boolean stopService(Long diverId) {
        return null;
    }

}
